﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for ConnectionDB
/// </summary>
public class ConnectionDB
{
    string cntr = @"Data Source=TRUNGTIN-PC;Initial Catalog=QLXeKhach;User ID=as;PassWord=123";
    SqlConnection con;
    SqlCommand cmd;
    public ConnectionDB()
    {
        con = new SqlConnection(cntr);
        con.Open();
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
    }

    public SqlCommand Cmd
    {
        set { cmd = value; }
        get { return cmd; }
    }

    public object ExecScalar(string sql, SqlParameter[] pa)
    {
        SqlCommand cmd = new SqlCommand(sql, con);
        //cmd.CommandType = CommandType.StoredProcedure;

        if (con.State == ConnectionState.Closed)
            con.Open();
        try
        {
            if (pa != null)
            {
                cmd.Parameters.AddRange(pa);
            }
            return cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            throw new Exception("Error: " + ex.Message);
        }
        finally
        {
            if (con.State == ConnectionState.Open)
                con.Close();
            cmd.Dispose();
        }
    }
}